package com.adcc.loadSheetForwarder.controller;

import com.adcc.loadSheetForwarder.configuration.AppConfiguration;
import com.adcc.loadSheetForwarder.configuration.UmeTripProperties;
import com.adcc.loadSheetForwarder.service.LoadSheetService;
import com.adcc.loadSheetForwarder.utils.LoadSheetException;
import com.adcc.loadSheetForwarder.utils.ResultCode;
import com.adcc.loadSheetForwarder.vo.*;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j
public class LoadSheetController {
    @Autowired
    AppConfiguration configuration;
    @Autowired
    UmeTripProperties properties;
    @Autowired
    LoadSheetService service;

    /**
     * 获取航班列表
     *
     * @param getFlightsVO
     * @return
     */
    @PostMapping("/getFlights")
    @ApiOperation(value = "/getFlights", notes = "获取航班列表", httpMethod = "POST")
    public ResultVO getFlights(@ApiParam(value = "获取航班列表参数", name = "getFlightsVO", required = true) @RequestBody GetFlightsVO getFlightsVO) {
        ResultVO vo = new ResultVO();
        String traceid = UUID.randomUUID().toString();
        log.info(String.format("traceid:%s,getFlights() params:%s,", traceid, getFlightsVO.toString()));
        try {
            if (!verifyGetFlightsParam(getFlightsVO)) {
                vo.setResultCode(ResultCode.SERVER_ERROR.getCode());
                vo.setResultMsg(ResultCode.SERVER_ERROR.getMsg());
                return vo;
            }
            List<FlightVO> flights = service.getFlights(getFlightsVO, traceid);
            vo.getData().put("flights", flights);
        } catch (LoadSheetException e) {
            vo.setResultCode(e.getReasonCode());
            vo.setResultMsg(e.getMessage());
        } catch (Exception e) {
            log.error(String.format("traceid:%s,getFlights() error", traceid), e);
            vo.setResultCode(ResultCode.SERVER_ERROR.getCode());
            vo.setResultMsg(ResultCode.SERVER_ERROR.getMsg());
        }
        return vo;
    }

    /**
     * 校验获取航班列表参数
     *
     * @param vo
     * @return
     */
    private boolean verifyGetFlightsParam(GetFlightsVO vo) {
        if (Strings.isNullOrEmpty(vo.getFlightNo())
                || Strings.isNullOrEmpty(vo.getFlightDate())
                || Strings.isNullOrEmpty(vo.getDeptAirport())) {
            return false;
        }
        return true;
    }

    /**
     * 获取舱单原文
     *
     * @return
     */
    @PostMapping(value = "/getOriginalLoadSheet")
    @ApiOperation(value = "/getOriginalLoadSheet", notes = "获取舱单原文", httpMethod = "POST")
    public ResultVO getOriginalLoadSheet(@ApiParam(value = "获取舱单原文参数", name = "getOriginalLoadSheetVO", required = true) @RequestBody GetOriginalLoadSheetVO getOriginalLoadSheetVO) {
        ResultVO vo = new ResultVO();
        String traceid = UUID.randomUUID().toString();
        log.info(String.format("traceid:%s,getOriginalLoadSheet() params:%s,", traceid, getOriginalLoadSheetVO.toString()));
        try {
            if (!verifyGetOriginalLoadSheetParam(getOriginalLoadSheetVO)) {
                vo.setResultCode(ResultCode.SERVER_ERROR.getCode());
                vo.setResultMsg(ResultCode.SERVER_ERROR.getMsg());
                return vo;
            }
            LoadSheetVO loadSheetVO = service.getOriginalLoadSheet(getOriginalLoadSheetVO, traceid);
            vo.getData().put("loadSheet", loadSheetVO);
        } catch (LoadSheetException e) {
            vo.setResultCode(e.getReasonCode());
            vo.setResultMsg(e.getMessage());
        } catch (Exception e) {
            log.error(String.format("traceid:%s,getOriginalLoadSheet() error", traceid), e);
            vo.setResultCode(ResultCode.SERVER_ERROR.getCode());
            vo.setResultMsg(ResultCode.SERVER_ERROR.getMsg());
        }
        return vo;
    }

    /**
     * 校验获取舱单原文参数
     *
     * @param vo
     * @return
     */
    private boolean verifyGetOriginalLoadSheetParam(GetOriginalLoadSheetVO vo) {
        if (Strings.isNullOrEmpty(vo.getFlightNo())
                || Strings.isNullOrEmpty(vo.getFlightDate())
                || Strings.isNullOrEmpty(vo.getDeptAirport())
                || Strings.isNullOrEmpty(vo.getArrAirport())
                || Strings.isNullOrEmpty(vo.getVersion())) {
            return false;
        }
        return true;
    }
}
